﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCtrl : MonoBehaviour
{
    private new Rigidbody2D rigidbody2D;

    private SpriteRenderer spriteRenderer;
    public float moveSpeed = 3;
    public float moveDirect;
    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        moveDirect = Input.GetAxis("Horizontal");
        // 左右移动
        rigidbody2D.velocity = new Vector2(moveDirect * moveSpeed, rigidbody2D.velocity.y);

        // 跳跃
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody2D.AddForce(Vector2.up * 200);
        }

        // 转向
        if (moveDirect < 0)
            spriteRenderer.flipX = true;
        else if(moveDirect > 0)
            spriteRenderer.flipX = false;
    }
}
